<?php

class NewsModel {

    private $conn = null;

    function __construct() {
        $this->conn = new mysqlconnect(Yaf_Registry::get('dbconfig'));
        $this->conn->connect();
    }

    public function fetchNewsByType($type) {
        $sql = 'SELECT * FROM news WHERE news_type=?';
        $result = $this->conn->fetch($sql,$type);

        return $result;
    }
    public function fetchNewById($id)
    {
        $sql = 'SELECT * FROM news WHERE id=?';
        return $this->conn->fetch($sql,$id);
    }

    public function fetchCaseDetailImageById($id)
    {
        $sql = 'SELECT * FROM case_image WHERE case_id=?';
        return $this->conn->fetch($sql,$id);
    }
    /*public function fetchInfo($offset, $pagesize) {
        $sql = 'SELECT info.id,title,content,insert_time,type_name,info_level,publish_status
                            FROM info JOIN info_type ON info.type_id=info_type.id WHERE publish_status=1 
                                ORDER BY info_level,insert_time DESC,id DESC limit ?,?';
        $result = $this->conn->fetch($sql, array($offset, $pagesize));
        return $result;
    }

    public function fetchInfoNum() {
        $sql = 'SELECT COUNT(info.id) num FROM info JOIN info_type ON info.type_id=info_type.id WHERE publish_status=1';
        $result = $this->conn->fetch($sql);

        return isset($result[0]['num']) ? $result[0]['num'] : 0;
    }

    public function fetchInfoByTypeId($typeid, $offset, $pagesize) {
        $sql = 'SELECT info.id,title,content,insert_time,type_name,info_level 
                            FROM info JOIN info_type ON info.type_id=info_type.id WHERE publish_status=1 AND type_id = ? 
                                ORDER BY info_level,insert_time DESC,id DESC limit ?,?';
        $result = $this->conn->fetch($sql, array($typeid, $offset, $pagesize));
        return $result;
    }

    public function fetchInfoNumByTypeId($typeid) {
        $sql = 'SELECT COUNT(info.id) num FROM info JOIN info_type ON info.type_id=info_type.id WHERE publish_status=1 AND type_id=?';
        $result = $this->conn->fetch($sql, $typeid);

        return isset($result[0]['num']) ? $result[0]['num'] : 0;
    }

    public function fetchInfoById($id) {
        $sql = 'SELECT title,content,insert_time FROM info WHERE id=?';
        $result = $this->conn->fetch($sql, $id);

        return $result;
    }

    public function fetchPendingInfo($offset, $pagesize) {
        $sql = 'SELECT info.id,title,content,insert_time,type_name,info_level,publish_status 
                            FROM info JOIN info_type ON info.type_id=info_type.id WHERE publish_status=0
                                ORDER BY info_level,insert_time DESC,id DESC limit ?,?';
        $result = $this->conn->fetch($sql, array($offset, $pagesize));
        return $result;
    }*/

    public function insertNews($data) {
        return $this->conn->insert('news', $data);
    }

    public function insertCaseType($data) {
        return $this->conn->insert('case_relation',$data);
    }
    
    public function insertCaseImage($data) {
        return $this->conn->insert('case_image',$data);
    }
    public function updateNews($data, $condition) {
        $this->conn->update('news', $data, $condition);
    }

    public function delete($table,$condition)
    {
        $this->conn->delete($table,$condition);
    }
}

?>
